<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>底部</title>
    <link rel="stylesheet" type="text/css" href="../../css/aui.css" />
	<style type="text/css">
		html,body{
		    height: 100%;
		}
	</style>
</head>
<body>
    <header class="aui-bar aui-bar-nav aui-bar-primary" id="aui-header">
        <div  id="header-title">首页</div>
    </header>
    <footer class="aui-nav" id="aui-footer">
        <ul class="aui-bar-tab">
            <li class="active-primary" id="tabbar1" tapmode onclick="randomSwitchBtn(this,'首页',0)">
                <span class="aui-iconfont aui-icon-home"></span>
                <p>首页</p>
            </li>
            <li id="tabbar2" tapmode onclick="randomSwitchBtn(this,'推送',1)">
                <span class="aui-iconfont aui-icon-message"></span>
                <p>推送</p>
            </li>
            <li id="tabbar3" tapmode onclick="randomSwitchBtn(this,'资源',2)">
                <span class="aui-iconfont aui-icon-file"></span>
                <p>资源</p>
            </li>
            <li id="tabbar4" tapmode onclick="randomSwitchBtn(this,'我的',3)">
                <span class="aui-iconfont aui-icon-my"></span>
                <p>我的</p>
            </li>
        </ul>
    </footer>
</body>
<script data-main="../../script/main" src="../../script/require.js"></script>
<script type="text/javascript">
	var DB;
	var dbName;
    apiready = function(){
    	require(["zepto","api","common"],function(){
	    	api.addEventListener({
		        name:'keyback'
	        },function(ret,err){
	        	api.closeWidget();
	        });
	        
	        initJpush();
	        initDB();
	        initRongCloud();
	        
        	var header = $api.byId('aui-header');
	        $api.fixStatusBar(header);
	        var headerPos = $api.offset(header);
	        var body_h = $api.offset($api.dom('body')).h;
	        var footer_h = $api.offset($api.byId('aui-footer')).h;
	        var status_bar_h = 20;
	        api.openFrameGroup ({
	            name: 'homepage',
	            scrollEnabled:true,
	            rect:{x:0, y:headerPos.h, w:'auto', h:body_h - headerPos.h - footer_h},
	            index:0,
	            preload:3,
	            frames:
	            [{
	                name: 'frame1_main',
	                url: '../main/main.html',
	                bounces:false
	            },{
	                name: 'frame2_push',
	                url: '../push/push.html',
	                bounces:false
	            },{
	                name: 'frame3_resource',
	                url: '../resource/resource.html',
	                bounces:false
	            },{
	                name: 'frame4_user',
	                url: '../user/user.html',
	                bounces:false
	            }]
	        }, function(ret, err){
	            var footer = $api.byId('aui-footer');
	            var footerAct = $api.dom(footer, '.aui-bar-tab li.active-primary');
	            $api.removeCls(footerAct, 'active-primary');
	            var name = ret.name;
	            var index = ret.index;
	            if(index==0){
	                $api.text($api.byId('header-title'),'首页');
	                $api.addCls($api.byId('tabbar1'), 'active-primary');
	            }else if(index==1){
	                $api.text($api.byId('header-title'),'推送');
	                $api.addCls($api.byId('tabbar2'), 'active-primary');
	            }else if(index==2){
	                $api.text($api.byId('header-title'),'资源');
	                $api.addCls($api.byId('tabbar3'), 'active-primary');
	            }else if(index==3){
	                $api.text($api.byId('header-title'),'我的');
	                $api.addCls($api.byId('tabbar4'), 'active-primary');
	            }
	        })
        });
    }
    function randomSwitchBtn(obj, name, index) {
        $api.text($api.byId('header-title'),name);
        var footer = $api.byId('aui-footer');
        var footerAct = $api.dom(footer, '.aui-bar-tab li.active-primary');
        $api.removeCls(footerAct, 'active-primary');
        $api.addCls(obj, 'active-primary');
        api.setFrameGroupIndex({
            name: 'homepage',
            index: index,
            scroll:true
        });
    }
    
    function initJpush(){
        var jpush = api.require('ajpush');
        jpush.resumePush();
        $zb.prefs.getPrefs('userInfo',function(ret){
            if(ret.value){
                var param = {};
                param.alias = JSON.parse(ret.value).push_id;
                param.tags = JSON.parse(ret.value).tags;
                jpush.init(function(ret, err) {
                    if (ret && ret.status) {
                        jpush.bindAliasAndTags(param, function(ret) {
                        });
                    }
                });
            }
        })
        jpush.setListener(function(ret) {
            console.log(JSON.stringify(ret));   
            executeAdd(ret);             
        });
    }
    
    function initRongCloud(){
        var rong = api.require('rongCloud2');
        rong.init(function(ret,err){
        });
    }
    
    function initDB(){
    	db = api.require('db');
		dbName = "zhibo_db";
    }
    
    function executeAdd(ret){
    	var id =  ret.id;
		var content = ret.content;
		var type = "";
		var from_user;
		if (ret.extra) {
			type = ret.extra.type;
			from_user = ret.extra.from_user;
		}
		if(type=="2"){
			db.openDatabase({
				name : dbName,
				path : 'fs://zhibo_db.db'
			}, function(ret, err) {
				if (!ret.status) {
					return;
				}
				/* 创建数据表 */
				var sql = "CREATE TABLE  IF NOT EXISTS `zhibo_messages`(`id` varchar(32), `content` varchar(255),`fromuser` varchar(100),  `datetime` timestamp  NOT NULL DEFAULT (datetime('now','localtime')))";
				db.executeSql({
					name : dbName,
					sql : sql
				}, function(ret, err) {
					if (!ret.status) {
						console.log('打开数据库失败' + err.msg);
						return;
					}
					/* 插入数据 */
					var sql2 = "insert into zhibo_messages (id,content,fromuser) values('" + id + "','" + content + "','" + from_user + "')";
					db.executeSql({
						name : dbName,
						sql : sql2
					}, function(ret, err) {
						if (!ret.status) {
							console.log('执行插入失败' + err.msg);
							return;
						}
					});
				});
			});
		}
    }
</script>
</html>
